package handler

import (
	"context"
	"github.com/go-errors/errors"
	"go.uber.org/zap"
	"takeout_server/basic/dao"
	__ "takeout_server/basic/proto/shopping"
)

type ShoppingServer struct {
	__.UnimplementedShoppingServerServer
}

func (s *ShoppingServer) ShoppingAdd(_ context.Context, req *__.ShoppingAddReq) (*__.ShoppingAddResp, error) {

	add, err := dao.ShoppingAdd(req.UserId, req.GoodId, req.Name, req.Price, req.Img, req.Sum, req.GoodType)
	if err != nil {
		zap.L().Info("购物车添加失败")
		return nil, err
	}
	if add.Id == 0 {
		zap.L().Info("购物车添加失败失败")
		return nil, err
	}
	return &__.ShoppingAddResp{ShoppingId: int32(add.Id)}, nil
}

func (s *ShoppingServer) ShoppingUpdate(_ context.Context, req *__.ShoppingUpdateReq) (*__.ShoppingUpdateResp, error) {

	if !dao.ShoppingUpdate(req.Id, req.Sum) {
		zap.L().Info("购物车修改数量失败失败")
		return nil, errors.New("修改数量失败")
	}

	return &__.ShoppingUpdateResp{Shopping: true}, nil
}
